import graphene
from flask import Flask
from flask_graphql import GraphQLView

from sqlalchemy import create_engine,Column,String
from sqlalchemy.engine import interfaces
from sqlalchemy.orm import scoped_session
from sqlalchemy.orm.session import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from graphene_sqlalchemy import SQLAlchemyObjectType,SQLAlchemyConnectionField

db = create_engine('mysql+pymysql://root:123@localhost:3306/graphql')
db_session = scoped_session(sessionmaker(bind=db))
Base = declarative_base()
Base.query = db_session.query_property()  # 获取查询对象

class EmployeeModel(Base):
    __tablename__ = 't_employee'
    id = Column(String(128),primary_key= True)
    name = Column(String(64))

class Employee(SQLAlchemyObjectType):
    class Meta:
        model = EmployeeModel
        interfaces = (graphene.relay.Node,)

# class Dept(SQLAlchemyObjectType):
#   class Meta:
#         model = EmployeeModel
#         interfaces = (graphene.relay.Node,)


class Query(graphene.ObjectType):
    node = graphene.relay.Node.Field()
    emps = SQLAlchemyConnectionField(Employee.connection)
    # depts = SQLAlchemyConnectionField(Dept.connection)
    '''
    before: String <arrayconnection:num>加密成base64    从1数
        从某个位置开始，要前面的数据
    after: String <arrayconnection:num>加密成base64    从0数
        从某个位置开始，要后面的数据
    first: Int
        前几个
    last: Int
        后几个
    '''
if __name__ =='__main__':
    schema = graphene.Schema(query=Query)
    app = Flask(__name__)
    app.add_url_rule('/graphql',view_func=GraphQLView.as_view('grapql',schema=schema, graphiql=True))
    app.run(debug=True)